package com.example.demo.controller;


import com.example.demo.entity.InstrumentCollection;
import com.example.demo.entity.LikeNoteComment;
import com.example.demo.service.LikeNoteCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("likeNoteComment")
public class LikeNoteCommentController {

    @Autowired
    private LikeNoteCommentService likeNoteCommentService;

    // 添加点赞(成功或失败的状态被放置在响应的success字段中，同时有一个message字段提供操作结果的描述。)
    @PostMapping("/add")
    public Map<String, Object> insertLikeNoteComment(@RequestBody LikeNoteComment likeNoteComment) {
        boolean success = likeNoteCommentService.insertLikeNoteComment(likeNoteComment);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);  //响应的success字段
        response.put("message", success ? "点赞成功" : "点赞失败");
        return response;
    }

    // 取消点赞
    @DeleteMapping("/delete")
    public Map<String, Object> deleteLikeNoteComment(@RequestBody LikeNoteComment likeNoteComment) {
        boolean success = likeNoteCommentService.deleteLikeNoteComment(likeNoteComment);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "取消点赞成功" : "取消点赞失败");
        return response;
    }

    // 检查是否点赞(这个接口接收user_id和note_comment_id作为参数，并返回用户是否已经点赞过该评论。)
    @GetMapping("/check")
    public Map<String, Object> checkIfLike(@RequestParam Integer user_id, @RequestParam Integer note_comment_id) {
        boolean like = likeNoteCommentService.checkIfLike(user_id, note_comment_id);
        Map<String, Object> response = new HashMap<>();
        response.put("like", like);
        return response;
        //创建了一个HashMap实例，它实现了Map接口，用于存储键值对。response变量被用作存储HTTP响应数据的容器，
        //其中键是String类型的，而值是Object类型的，这意味着可以在其中存储任何类型的对象。
    }

    //根据评论id统计点赞数量(接受一个请求参数note_comment_id，它是通过请求的查询参数传递的)
    @GetMapping("/count")
    public ResponseEntity<Integer> countLike(
            @RequestParam Integer note_comment_id) {
        Integer count = likeNoteCommentService.countLike(note_comment_id);
        return ResponseEntity.ok(count); //如果请求成功，客户端将收到一个包含点赞数量的HTTP响应。
    }

}
